import {Component, OnInit, ViewChild} from '@angular/core';
import {FormBuilder} from '@angular/forms';
import {NziModalService} from 'ng-zorro-iop/modal/nzi-modal.service';
import {RmService} from '../rm.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-rm-list',
  templateUrl: './rm-list.component.html',
  styleUrls: ['./rm-list.component.css']
})
export class RmListComponent implements OnInit {
  @ViewChild('resetFormComponent') resetFormComponent;
  isLoading = true;
  allChecked = false;
  indeterminate = false;
  dataSet = [];
  _current = 1;
  _pageSize = 10;
  _total = 2;
  applicant = '';
  displayData = [];
  checkedNumber = 0;
  deleteButton = true;

  ngOnInit() {
    this.refreshData();
  }
  refreshPage() {
    this.applicant = '';
    this.refreshData();
  }

  del(id): void {
    const mid = this.message.loading('正在删除中', {nzDuration: 0}).messageId;
    this.mainService.delete(id).subscribe(response => {
      if (response === false) {
        this.message.error('删除失败！');
        this.message.remove(mid);
      } else if (response === true) {
        this.refreshData();
        this.message.success('删除成功！');
        this.message.remove(mid);
      }
    });
  }

  currentPageDataChange($event: Array<any>): void {
    this.displayData = $event;
  }

  refreshData(reset = false) {
    if (reset) {
      this._current = 1;
    }
    this.isLoading = true;
    const current = this._current - 1;
    this.mainService.getPageData(current, this._pageSize, this.applicant).subscribe(datas => {
      this.dataSet = datas.content || [];
      this._total = datas.totalElements;
      this.isLoading = false;
    });
  }

  searchFun() {
    this.refreshData();
  }

// --------------------------------------------------------------------------------------------------------------------------

  constructor(private fb: FormBuilder,
              private modalService: NziModalService,
              private mainService: RmService,
              private message: NzMessageService) {
  }
}
